const {
  getCategoryList
} = require("../../api/api.js")
const { _navigateTo } = require('../../utils/util.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    img_host: '',
    categoryList: [],
    cloneList:[]
  },
  onChange(event) {
    //获取到的下表
    let index = event.detail;
    let cloneList = [...this.data.categoryList];
    let originList = this.data.categoryList;
    if (index === 0) {
      this.setData({
        cloneList: originList
      })
    }else{
      this.setData({
        cloneList: [originList[index - 1]]
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    getCategoryList().then(res => {
      console.log("分类页数据");
      console.log(res)
      this.setData({
        categoryList: res.data,
        cloneList:res.data
      })
      console.log(this.data.categoryList)
      console.log(this.data.cloneList)
    }).catch(err => {
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   * page页面加载数据时使用这个
   */
  onReady: function() {
    this.setData({
      img_host: app.img_host
    })
  },
  /**
   *  至指定分类列表页
   */
  toCategoryList(e) {
    const { id } = e.currentTarget.dataset;
    console.log("分类页传递的id")
    console.log(id)
    app.id = id
    _navigateTo(`./category-list/index?id=${id}`);
  }
})